package org.mebusw.dao.proxy;

import java.util.List;
import org.mebusw.dao.IGameTableDAO;
import org.mebusw.dao.impl.IGameTableDAOImpl;
import org.mebusw.dbc.DataBaseConnection;
import org.mebusw.struts.davinci.bean.GameBrief;
import org.mebusw.struts.vo.GameTable;

/**
 * 
 * JSP should use DAOFactory to generate instance of IGameTableDAOProxy.
 * IGameTableDAOProxy create DB connection, then access DB.
 * 
 * @author j1shen
 * @see IGameTableDAOImpl
 * 
 */
public class IGameTableDAOProxy implements IGameTableDAO {
	private DataBaseConnection dbc = null;
	private IGameTableDAO dao = null;
	private int count = 0;

	public IGameTableDAOProxy() {
		this.dbc = new DataBaseConnection();
		this.dao = new IGameTableDAOImpl(this.dbc.getConnection());
	}

	@Override
	public boolean doCreate(GameTable gameTable) throws Exception {
		boolean flag = false;
		try {
			flag = this.dao.doCreate(gameTable);

		} catch (Exception e) {
			throw e;
		} finally {
			this.dbc.close();
		}

		return flag;
	}

	@Override
	public boolean doDelete(int uid) throws Exception {
		boolean flag = false;
		try {
			flag = this.dao.doDelete(uid);
		} catch (Exception e) {
			throw e;
		} finally {
			this.dbc.close();
		}
		return flag;
	}

	@Override
	public boolean doUpdate(GameTable gameTable) throws Exception {
		boolean flag = false;
		try {
			flag = this.dao.doUpdate(gameTable);
		} catch (Exception e) {
			throw e;
		} finally {
			this.dbc.close();
		}
		return flag;
	}

	@Override
	public GameTable findById(int gid) throws Exception {
		GameTable gameTable = null;
		try {
			gameTable = this.dao.findById(gid);
		} catch (Exception e) {
			throw e;
		} finally {
			this.dbc.close();
		}
		return gameTable;
	}

	public List<GameBrief> findAll()
			throws Exception {
		List<GameBrief> allGames = null;
		try {
			allGames = this.dao.findAll();
			this.count = allGames.size();

		} catch (Exception e) {
			throw e;
		} finally {
			this.dbc.close();
		}
		return allGames;
	}

	@Override
	public int getAllCount(String keyword) throws Exception {
		// TODO get the count by given keyword
		return 0;
	}

	@Override
	public int getAllCount() throws Exception {
		return this.count;
	}

}
